﻿using eProcurement.BLL;
using eProcurement.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProcurementProject.members.userprofile
{
    public partial class Profile : System.Web.UI.Page
    {
        Manager manager = new Manager();

        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !IsPostBack )
            {
                string currentuser = Context.User.Identity.Name;

                Hydrate( manager.GetAppUser( currentuser ) );
            }
        }

        private void Hydrate( AppUser user )
        {
            txtCompanyName.InnerText = user.CompanyName;
            txtWebsite.Value = user.Website;
            txtAddress1.Value = user.Address1;
            txtAddress2.Value = user.Address2;
            txtCity.Value = user.City;
            txtDescription.Value = user.Description;

            //Oersonal Details
            txtFirstname.Value = user.FirstName;
            txtLastname.Value = user.LastName;
            txtJobTitle.Value = user.JobTitle;
            txtTelephone.Value = user.Telephone;
            txtFax.Value = user.Fax;
        }

        protected void btnSave_Click( object sender, EventArgs e )
        {
            AppUser user = new AppUser();
            user.Website = txtWebsite.Value;
            user.Address1 = txtAddress1.Value;
            user.Address2 = txtAddress2.Value;
            user.City = txtCity.Value;
            user.Description = txtDescription.Value;

            user.FirstName = txtFirstname.Value;
            user.LastName = txtLastname.Value;
            user.JobTitle = txtJobTitle.Value;
            user.Telephone = txtTelephone.Value;
            user.Fax = txtFax.Value;

            user.CompanyName = txtCompanyName.InnerText;
            user.Username = Context.User.Identity.Name;

            if ( manager.UpdateUser( user ) )
            {
                tipSaved.Visible = true;
            }
        }
    }
}